package com.yanqu.road.server.cmd.minewar.task;

import com.yanqu.road.server.pb.MineWarPb;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.minewar.UserMinePit;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.pb.minewar.MineWarProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.BaseTask;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.player.MineWarMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.List;

public class SearchUserPitListTask extends BaseTask {

    private long userAliasId;

    private UserInfo searchUserInfo;

    private List<UserMinePit> pitList;

    public SearchUserPitListTask(YanQuMessage packet, GamePlayer player) {
        super(packet, player);
    }

    @Override
    protected boolean parseData() throws Exception {
        MineWarProto.MineSearchUserMinePitReqMsg reqMsg = MineWarProto.MineSearchUserMinePitReqMsg.parseFrom(packet.getBytes());
        userAliasId = reqMsg.getUserAliasId();
        return true;
    }

    @Override
    protected void execute() {
        searchUserInfo = UserMgr.getUserInfoByAliasId(userAliasId);
        if(null == searchUserInfo){
            ret = GameErrorCode.E_USER_NO_FOUND;
            return;
        }
        //判断是否足够侦查道具
        if(!player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(GameConfig.MINE_WAR_SEARCH_CONSUME),
                eLogMoneyType.MineWar, eLogMoneyType.MineWarDetect)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        pitList = MineWarMgr.getUserMinePitList(searchUserInfo.getUserId());
    }

    @Override
    protected void sendData(GamePlayer player) {
        MineWarProto.MineSearchUserMinePitRespMsg.Builder respMsg = MineWarProto.MineSearchUserMinePitRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(0 == ret){
            PlayerProto.PlayerSimpleTempMsg.Builder userMsg = PlayerPb.parsePlayerSimpleTempMsg(searchUserInfo);
            if(null != userMsg){
                respMsg.setUserData(userMsg);
            }

            MineWarProto.MinePitListMsg.Builder listMsg = MineWarPb.parseMinePitListMsg(pitList);
            respMsg.setMinePitList(listMsg);
        }
        player.sendPacket(Protocol.U_MINE_WAR_SEARCH_USER_DATA, respMsg);
    }
}
